/*
 * Copyright (c) 2019.  鼎捷软件股份有限公司.All rights reserved.
 */

package com.digiwin.dap.middleware.exception;

import com.fasterxml.jackson.annotation.JsonIgnore;
import org.springframework.http.HttpStatus;

/**
 * 错误信息对象
 * Created by chenzwd on 2018/2/23.
 */

public class AppError {

    public AppError() {
    }

    public static <T> AppError genError(Exception e) {
        AppError result = new AppError();
        result.setCode(HttpStatus.INTERNAL_SERVER_ERROR);
        result.setMessage(e.getMessage());
        result.setExceptionDetail(e);
        return result;
    }


    public static <T> AppError genError(String message) {
        AppError result = new AppError();
        result.setCode(HttpStatus.INTERNAL_SERVER_ERROR);
        result.setMessage(message);
        return result;
    }



    private HttpStatus code;
    private String message;
    private String path;
    @JsonIgnore
    private String exceptionDetail;

    public HttpStatus getCode() {
        return code;
    }

    public void setCode(HttpStatus code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public String getExceptionDetial() {
        return exceptionDetail;
    }


    static String getExceptionDetail(Exception e) {
        if (e == null) {
            return "";
        }
        StringBuffer msg = new StringBuffer("null");
        if (e != null) {
            msg = new StringBuffer("");
            String message = e.toString();
            int length = e.getStackTrace().length;
            if (length > 0) {
                msg.append(message + System.getProperty("line.separator"));
                for (int i = 0; i < length; i++) {
                    msg.append(e.getStackTrace()[i] + System.getProperty("line.separator"));
                }
            } else {
                msg.append(message);
            }
        }
        return msg.toString().trim();
    }

    /**
     * @param e
     */
    public void setExceptionDetail(Exception e) {

        this.exceptionDetail = getExceptionDetail(e);
    }
}